//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    ProjectBox:false,
    hasMoreTalk:true,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  floatTap:function () {
      this.setData({
          ProjectBox:!this.data.ProjectBox
      })
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.login()
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.login()
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.login()
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    this.getData()
    this.GamesObj()
  },
  //用户访问小程序接口
  userApp:function (e) {
      var that = this
      if(app.globalData.userInfo){
          wx.request({
              url: 'https://caige1.sanliwenhua.com/program/user_app?app=9',
              method: "post",
              data:{
                  x_token:app.globalData.userInfo.token,
                  appid: e.currentTarget.dataset.appid,
              },
              success: function (res) {
                  console.log(res)
                  if(res.data.code == 200){
                      /*wx.showToast({
                          title: "成功了录取记录",
                          icon:"none",
                          duration: 2000
                      })*/
                      that.NumberGamesObj(e.currentTarget.dataset.appid)
                  }
              },
              fail:function (res) {

              }
          })
      }
  },
  //玩游戏数量处理
  GamesObj:function () {
        var that = this
        var myTime = new Date();
        var today = myTime.getDate();
        wx.getStorage({
            key: 'NumberGames',
            success: function (res) {
                var NumberGames = JSON.parse(res.data)
                //console.log('ChallengeObj', ChallengeObj)
                if (today != NumberGames.day) {
                    NumberGames = {
                        day: today,
                        GamesTime: 0,
                        GamesAppid: []
                    }
                }
                wx.setStorage({
                    key: 'NumberGames',
                    data: JSON.stringify(NumberGames)
                })
                app.globalData.NumberGames = NumberGames
                that.setData({
                    GamesTime:NumberGames.GamesTime
                })

            }, fail: function (res) {
                //console.log(res)
                var NumberGames = {
                    day: today,
                    GamesTime: 0,
                    GamesAppid: []
                }
                wx.setStorage({
                    key: 'NumberGames',
                    data: JSON.stringify(NumberGames)
                })
                app.globalData.NumberGames = NumberGames
                that.setData({
                    GamesTime:NumberGames.GamesTime
                })
            }
        })

    },
  NumberGamesObj:function (e) {
        var that = this
        var myTime = new Date();
        var today = myTime.getDate();
        wx.getStorage({
            key: 'NumberGames',
            success: function (res) {
                var NumberGames = JSON.parse(res.data)
                //console.log('ChallengeObj', ChallengeObj)
                var GamesAppid = NumberGames.GamesAppid;
                if (today != NumberGames.day){
                    NumberGames = {
                        day: today,
                        GamesTime: 0,
                        GamesAppid: []
                    }
                }else{
                    if(GamesAppid.length>0){
                        for(var i=0;i<GamesAppid.length;i++){
                            if(NumberGames.GamesAppid[i]==e){
                                NumberGames = {
                                    day: today,
                                    GamesTime: NumberGames.GamesTime,
                                    GamesAppid: GamesAppid
                                }
                                break;
                            }else {
                                if(i+1==GamesAppid.length){
                                    GamesAppid.push(e)
                                    NumberGames = {
                                        day: today,
                                        GamesTime: NumberGames.GamesTime+1,
                                        GamesAppid: GamesAppid
                                    }
                                }
                            }
                        }
                    }else{
                        GamesAppid.push(e)
                        NumberGames = {
                            day: today,
                            GamesTime: NumberGames.GamesTime+1,
                            GamesAppid: GamesAppid
                        }
                    }
                }
                wx.setStorage({
                    key: 'NumberGames',
                    data: JSON.stringify(NumberGames)
                })
                app.globalData.NumberGames = NumberGames
                that.setData({
                    GamesTime:NumberGames.GamesTime
                })

            }, fail: function (res) {
                //console.log(res)
                var NumberGames = {
                    day: today,
                    GamesTime: 0,
                    GamesAppid:[]
                }
                wx.setStorage({
                    key: 'NumberGames',
                    data: JSON.stringify(NumberGames)
                })
                app.globalData.NumberGames = NumberGames
                that.setData({
                    GamesTime:NumberGames.GamesTime
                })
            }
        })

    },
  //登录接口
  login:function () {
        var that = this
        console.log(app.globalData)
        if(app.globalData.userInfo){
            var dataSend = {
                code: app.globalData.code,
                icon: app.globalData.userInfo.avatarUrl,
                nickname: app.globalData.userInfo.nickName
            }
            console.log(app.globalData.code)
            wx.request({
                url: 'https://caige1.sanliwenhua.com/program/wx/onLogin?app=9',
                method: "get",
                data: dataSend,
                success: function (res) {
                    console.log(res)
                    if(res.data.code == 200){
                        var token = res.data.data.x_token;
                        var gold = res.data.data.values.gold;
                        var userid = res.data.data.values.user_id;
                        app.globalData.userInfo.token = token;
                        app.globalData.userInfo.gold = gold;
                        app.globalData.userInfo.userid = userid
                    }
                },
                fail:function (res) {
                    wx.showToast({
                        title: res.data.message,
                        icon:"none",
                        duration: 2000
                    })
                }
            })
        }
    },
  //数据请求
  getData:function () {
      var that = this
      //轮播数据请求
      wx.request({
          url: 'https://caige1.sanliwenhua.com/program/column?app=9',
          method: "get",
          success: function (res) {
              if (res.data.code = 200) {
                  console.log(res.data)
                  var  data= res.data.data;
                  var imgUrls=[];
                  var indexAdv = []
                  for(var i=0;i<data.length;i++){
                      /*if(data[i].desc ==" "){
                          data[i].desc = "我是悟空 弹一弹真的很好玩"
                      }*/
                      if(data[i].type=="index_Broadcast"){
                          imgUrls.push(data[i])
                      }
                      if(data[i].type=="index_adv"){
                          indexAdv.push(data[i])
                      }
                  }
                  that.setData({
                      imgUrls: imgUrls,
                      indexAdv:indexAdv
                  })
              }
          }
      })
      //热门数据请求
      wx.request({
          url: 'https://caige1.sanliwenhua.com/program/relation/is_hot?app=9',
          method: "get",
          data:{
              count:100
          },
          success: function (res) {
              if (res.data.code = 200) {
                  console.log("热门",res.data.data.data)
                  var data = res.data.data.data
                  var hotData = [];
                  var datalist = [];
                  for(var i=0;i<data.length;i++){
                      if(i%3!=0){
                          datalist.push(data[i])
                      }else{
                          if (i > 0) {
                              hotData.push(datalist)
                          }
                          datalist = [data[i]]
                      }
                  }
                  hotData.push(datalist)
                  that.setData({
                      hotData: hotData
                  })
              }
          }
      })
      //最新数据请求
      wx.request({
          url: 'https://caige1.sanliwenhua.com/program/relation/is_new?app=9',
          method: "get",
          success: function (res) {
              if (res.data.code = 200) {
                  console.log("推荐",res.data.data.data)
                  var data = res.data.data.data
                  var recData = [];
                  var datalist = [];
                  for(var i=0;i<data.length;i++){
                      if(data[i].app.players*1>=10000){
                          data[i].app.players = (data[i].app.players*1/10000).toFixed(1)+"万"
                      }
                  }
                  for(var i=0;i<data.length;i++){
                      if(i%3!=0){
                          datalist.push(data[i])
                      }else{
                          if (i > 0) {
                              recData.push(datalist)
                          }
                          datalist = [data[i]]
                      }
                  }
                  recData.push(datalist)
                  that.setData({
                      recData: recData
                  })
              }
          }
      })
      //大家都在玩数据请求
      wx.request({
          url: 'https://caige1.sanliwenhua.com/program/category/all?app=9',
          method: "get",
          success: function (res) {
              if (res.data.code = 200) {
                  console.log("大家都在玩",res.data.data.data)
                  var data = res.data.data.data
                  for(var i=0;i<data.length;i++){
                      if(data[i].app.players*1>=10000){
                          data[i].app.players = (data[i].app.players*1/10000).toFixed(1)+"万"
                      }
                  }
                  that.setData({
                      InterData: data
                  })
              }
          }
      })

  },
  getUserInfo: function(e) {
    console.log(e)
      if(e.detail.userInfo){
          app.globalData.userInfo = e.detail.userInfo
          this.login()
          this.setData({
              userInfo: e.detail.userInfo,
              hasUserInfo: true
          })
      }
  },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom:function () {
        var that = this
        //大家都在玩数据请求
        wx.request({
            url: 'https://caige1.sanliwenhua.com/program/category/all?app=9',
            method: "get",
            data:{
                count:100
            },
            success: function (res) {
                if (res.data.code = 200) {
                    console.log("大家都在玩",res.data.data.data)
                    var data = res.data.data.data
                    for(var i=0;i<data.length;i++){
                        if(data[i].app.players*1>=10000){
                            data[i].app.players = (data[i].app.players*1/10000).toFixed(1)+"万"
                        }
                    }
                    that.setData({
                        InterData: data,
                        hasMoreTalk:false
                    })
                }
            }
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        var that = this
        if (res.from === 'button') {

        }
        return {
            title: '小姐姐们发现了一个超级好玩的！',
            path: 'pages/index/index',
            imageUrl:'http://pbkva7be7.bkt.clouddn.com/image/tw_35.png',
            success: function (res) {
                wx.showToast({
                    title: '分享成功!',
                    icon: 'success',
                    duration: 2000
                })

            },fail: function (res) {
                console.log(res)
                wx.showToast({
                    title: '分享失败！',
                    image: '/image/errIcon.png',
                    duration: 2000
                })
            }
        }
    }
})
